package com.cyh.forum.services.impl;


import com.cyh.forum.common.AppResult;
import com.cyh.forum.common.ResultCode;
import com.cyh.forum.dao.ArticleReplyMapper;
import com.cyh.forum.exception.ApplicationException;
import com.cyh.forum.model.Article;
import com.cyh.forum.model.ArticleReply;
import com.cyh.forum.services.IArticleReplyService;
import com.cyh.forum.services.IArticleService;
import com.cyh.forum.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleReplyServiceImpl implements IArticleReplyService {

    //  注入mapper
    @Resource
    private ArticleReplyMapper articleReplyMapper;

    //  注入articleService层
    @Resource
    private IArticleService iArticleService;


    @Override
    public void create(ArticleReply articleReply) {
        //  非空校验           帖子,用户ID,帖子ID,回复内容不能为空
        if (articleReply == null
                || articleReply.getPostUserId() == null
                || articleReply.getArticleId() == null
                || StringUtil.isEmpty(articleReply.getContent())){
            //  打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //  设置默认值
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);


        articleReply.setLikeCount(0);
        articleReply.setState((byte) 0);
        articleReply.setDeleteState((byte) 0);
        Date date = new Date();
        articleReply.setCreateTime(date);
        articleReply.setUpdateTime(date);

        // 调用DAO层    写入数据库
        val row = articleReplyMapper.insertSelective(articleReply);
        //  校验是否创建回复成功
        if (row != 1){
            //  打印日志
            log.warn(ResultCode.FAILED_ARTICLE_REPLY_CREATE.toString());
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_REPLY_CREATE));
        }

        //  更新帖子表中的回复数
        iArticleService.addOneReplyCountById(articleReply.getArticleId());
        //  打印执行成功日志
        log.info("回复成功 !  articleID = " + articleReply.getArticleId() + ", userID = " + articleReply.getPostUserId());
    }

    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        //  非空校验
        if (articleId == null || articleId <= 0){
            //  打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //  校验帖子是否存在
        Article article = iArticleService.selectById(articleId);
        if (article == null || article.getDeleteState() != 0){
            //  打印日志
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }

        //  调用DAO层
        List<ArticleReply> articleReplies = articleReplyMapper.selectByArticleId(articleId);
        //  返回结果
        return articleReplies;
    }


}
